<template>
	<u-gap height="20"></u-gap>
	<template v-for="(item, index) in list" :key="item.id">
		<u-swipe-action :show="item.show" :index="index" @click="swipeClick" @open="swipeOpen" :options="options"
			:btn-width="100" bg-color="#f9f9f9">
			<view class="u-p-l-20 u-p-r-20" style="background-color: #f9f9f9;">
				<view class="box-radius bgw u-p-28">
					<view class="u-flex">
						<view class="u-flex-1">
							<view class="u-font-32">
								<text>{{item.name}}</text>
								<text class="u-m-l-28">{{item.phone}}</text>
							</view>
							<u-gap height="10"></u-gap>
							<view class="u-content-color">
								{{item.province}}/{{item.city}}/{{item.district}}/{{item.address}}
							</view>
							<u-gap height="10"></u-gap>
							<view class="" v-if="item.defaulted == 0">
								<u-icon name="checkmark-circle-fill" color="#117B12" size="30" label="默认"></u-icon>
							</view>
							<!-- <view class="u-tips-color u-font-24" v-else>
								设为默认
							</view> -->
						</view>
						<view class="" v-if="from == 'order'">
							<u-button @click="chooseAddress(item)" type="primary"
								size="mini" shape="circle" class="u-m-l-16">选择</u-button>
						</view>
					</view>

				</view>
			</view>
		</u-swipe-action>
		<u-gap height="20"></u-gap>
	</template>

	<u-gap height="20"></u-gap>
	<g-loadmore :status="loadmoreStatus" nodata-img="/static/images/nodata-address.png" nodata-text="">
		<template #nodata>
			<view class="u-text-center">
				<text class="u-tips-color">暂无相关地址，</text>
				<text class="color-primary" @click="navLogin('/pagesSub/editAddress/editAddress')">去添加</text>
			</view>
		</template>
	</g-loadmore>
	<u-gap height="150"></u-gap>
	<view class="fixed-bottom u-p-l-48 u-p-r-48 u-p-t-20 u-p-b-20 bgw">
		<view class="u-flex">
			<view class="u-flex-1 u-p-l-10 u-p-r-10">
				<u-button @click="navLogin('/pagesSub/editAddress/editAddress')" type="primary"
					shape="circle">新增地址</u-button>
			</view>
		</view>
	</view>

</template>
<script setup>
	import store from '@/store/index.js';
	import mixins from '@/common/js/mixins.js';
	import mixinList from '@/common/js/mixinList.js';
	import {
		ref,
		reactive,
		computed,
		watch,
		getCurrentInstance
	} from 'vue'
	import {
		onReady,
		onLoad,
		onShow,
		onPageScroll
	} from '@dcloudio/uni-app'
	const {
		proxy,
	} = getCurrentInstance()
	const {
		nav,
		navLogin
	} = proxy.$util;
	const props = defineProps({
		from: {
			type: String,
			default: '',
		}
	})
	const {
		total,
		current_page,
		page_size,
		last_page,
		apiPath,
		loading,
		list,
		searchParams,
		getList,
		getNextList,
		loadmoreStatus,
		searchClear,
		searchConfirm
	} = mixinList();
	apiPath.value = 'getAddress'

	const options = [{
			text: '删除',
			style: {
				backgroundColor: '#F1F1F1',
				color: '#666666',
				fontSize: '24upx',
				borderRadius: '20upx',
				margin: '0 5upx'
			}
		},
		{
			text: '编辑',
			style: {
				backgroundColor: '#F2FDF2',
				color: '#117B12',
				fontSize: '24upx',
				borderRadius: '20upx',
				margin: '0 5upx'
			}
		}
	];
	const swipeOpen = (index) => {
		list[index].show = true;
		list.map((val, idx) => {
			if (index != idx) {
				list[idx].show = false;
			}
		})
	}
	const swipeClick = (index, menuIndex) => {
		let sid = list[index].sid;
		if(menuIndex == 0){
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			proxy.$u.api.deleteAddress({
				sidList:[sid]
			}).then(res => {
				let {
					code,
					msg,
					data
				} = res;
				if (code == 200) {
					list.splice(index, 1);
					uni.$u.toast('删除成功');
				} else {
					uni.$u.toast(msg || '错误');
				}
			})
		}
		if(menuIndex == 1){
			navLogin('/pagesSub/editAddress/editAddress?sid='+sid);
			list[index].show = false;
		}
	}
	const chooseAddress = (v) =>{
		uni.$emit('choose_address', v)
		uni.navigateBack();
	}


	onReady(() => {

	})
	onLoad(async (d) => {
		
	})
	onShow(async () => {
		await getList(1)
		
		// uni.$off('address_update');
		// uni.$once('address_update', (v) => {
		// 	getList(1)
		// })

	})
	onPageScroll(e => {

	})
</script>
<style>

</style>
<style lang="scss" scoped>
	.fixed-bottom {
		background-color: #ffffff;
		box-shadow: 0 -4upx 20upx 0 rgba(0, 0, 0, 0.06);
		bottom: 0;
		z-index: 999;
	}
</style>